package test;

import java.io.*;

public class NumberTriangle {
    public int readFile(int[][] triangledata) {
        int triangleNum = 0;
        try {

            FileReader fr = new FileReader("E:\\JavaProject\\IdeaProjects\\JavaWeb\\test\\src\\test\\input.txt");
            String str;
            int count = 0;
            BufferedReader br = new BufferedReader(fr);
            while ((str = br.readLine()) != null) {
                if (count == 0) {
                    String substring = str.substring(0, str.length());
                    triangleNum = Integer.valueOf(substring);
                    count++;
                } else {
                    String[] split = str.split(" ");
                    for (int i = 1; i <= split.length; i++) {
                        triangledata[count][i] = Integer.valueOf(split[i - 1]);
                    }
                    count++;
                }
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return triangleNum;
    }

    public void printTridata(int triangleNum, int[][] triangledata) {
        System.out.println("文件input.txt的内容为：");
        System.out.println(triangleNum);
        for (int i = 1; i <= triangleNum; i++) {
            for (int j = 1; j <= i; j++) {
                if (j == i) {
                    System.out.print(triangledata[i][j]);
                } else {
                    System.out.print(triangledata[i][j] + " ");
                }
            }
            System.out.println();
        }
        System.out.println("========================");
    }

    public int getMax(int triangleNum, int[][] triangledata, int[][] path) {
        for (int row = triangleNum - 1; row >= 0; row--) {
            for (int col = 1; col <= row; col++) {
                if (triangledata[row + 1][col] > triangledata[row + 1][col + 1]) {
                    triangledata[row][col] += triangledata[row + 1][col];
                    path[row + 1][col] = triangledata[row + 1][col];
                } else {
                    triangledata[row][col] += triangledata[row + 1][col + 1];
                    path[row + 1][col + 1] = triangledata[row + 1][col + 1];
                }
            }
        }
        return triangledata[1][1];
    }

    public void writeFile(int max, int[] num, int triangleNum) {
        try {
            File file = new File("E:\\JavaProject\\IdeaProjects\\JavaWeb\\test\\src\\test\\output.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fw = new FileOutputStream(file);
            String s = String.valueOf(max);
            fw.write(s.getBytes());
            fw.write("\r\n".getBytes());
            for (int i = 0; i < triangleNum; i++) {
                String s1 = String.valueOf(num[i]);
                fw.write(s1.getBytes());
                fw.write(" ".getBytes());
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void GetPath(int[][] tridatacopy, int triangleNum, int[][] path, int[] num) {
        System.out.print(tridatacopy[1][1] + "->");
        num[0] = tridatacopy[1][1];
        for (int row = 1, col = 1; row <= triangleNum - 1; ) {
            if (path[row + 1][col] > path[row + 1][col + 1]) {
                if (row <= triangleNum - 2) {
                    System.out.print(tridatacopy[row + 1][col] + "->");
                } else {
                    System.out.println(tridatacopy[row + 1][col]);
                }
                num[row] = tridatacopy[row + 1][col];
                row++;

            } else {
                if (row <= triangleNum - 2) {
                    System.out.print(tridatacopy[row + 1][col + 1] + "->");
                } else {
                    System.out.println(tridatacopy[row + 1][col + 1]);
                }
                num[row] = tridatacopy[row + 1][col + 1];
                row++;
                col++;
            }
        }
    }

    public void copyArray(int[][] triangledata, int[][] triangledatacopy, int triangleNum) {
        for (int i = 1; i <= triangleNum; i++) {
            for (int j = 1; j <= i; j++) {
                triangledatacopy[i][j] = triangledata[i][j];
            }
        }
    }

    public static void main(String[] args) {
        NumberTriangle nt = new NumberTriangle();
        int[][] triangledata = new int[20][20];
        int[][] triangledatacopy = new int[20][20];
        int[][] path = new int[20][20];
        int[] num = new int[20];
        int triangleNum = nt.readFile(triangledata);
        nt.printTridata(triangleNum, triangledata);
        nt.copyArray(triangledata, triangledatacopy, triangleNum);
        int max = nt.getMax(triangleNum, triangledata, path);
//        nt.printPath(path);
        System.out.println("最大值是:" + max);
        System.out.println("路径为：");
        nt.GetPath(triangledatacopy, triangleNum, path, num);
        nt.writeFile(max, num, triangleNum);
    }

    /*private void printPath(int[][] path) {
        for (int[] ints : path) {
            for (int i = 0; i < ints.length; i++) {
                System.out.print(ints[i]+"  ");
            }
            System.out.println();
        }
    }*/
}
